<template>
  <!-- 新增或修改数据 -->
  <el-dialog :visible.sync="dialogFormVisible">
    <!-- 输入框 -->
    <el-form
      :model="ruleForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <!-- 标题 -->
      <el-form-item label="标题" prop="title">
        <el-input
          type="text"
          autocomplete="off"
          v-model.number="ruleForm.title"
        ></el-input>
      </el-form-item>
      <el-form-item label="摘要" prop="description">
        <el-input
          type="text"
          v-model="ruleForm.description"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <div>
        <span style="margin-left: 60px">类型</span>
        <el-select
          style="margin-left: 10px"
          v-model="ruleForm.type"
          placeholder="请选择"
        >
          <el-option
            v-for="item in types"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </div>
      <!-- 按钮 -->
      <el-form-item style="margin-top: 10px">
        <el-button type="primary" @click="submit">提交</el-button>
        <el-button @click="dialogFormVisible = false">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
export default {
  name: "homeNewsEdit",
  components: {},
  data() {
    return {
      types: [
        {
          value: "banner",
          label: "轮播图",
        },
        {
          value: "category",
          label: "分类",
        },
        {
          value: "news",
          label: "新闻",
        },
        {
          value: "recommend",
          label: "为您推荐",
        },
      ],
      dialogFormVisible: false,
      ruleForm: {
        title: "",
        url: "",
        img: "",
        type: "news",
      },
      rules: {
        title: { required: true, message: "标题不能为空", trigger: "blur" },
        description: {
          required: true,
          message: "摘要不能为空",
          trigger: "blur",
        },
      },
    };
  },
  methods: {
    add() {
      this.dialogFormVisible = true;
      this.ruleForm = { type: "news" };
    },
    //编辑
    edit(row) {
      this.dialogFormVisible = true;
      this.ruleForm = row;
      this.ruleForm.indexId = row._id;
    },
    //新增或者修改
    submit() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          this.$http({
            url: this.ruleForm.indexId ? "/admin/change" : "/admin/add",
            method: this.ruleForm.indexId ? "PUT" : "POST",
            data: this.ruleForm,
          }).then((res) => {
            console.log(res);
            if (!res.errorMsg) {
              this.$emit("load", this.ruleForm.indexId ? undefined : 1);
              this.dialogFormVisible = false;
              this.$message({
                type: "success",
                message: "数据处理成功",
              });
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
  created() {},
  mounted() {},
};
</script>
  
<style scoped>
.form {
  margin-left: 80px;
  margin-top: 50 px;
}
.searchContent {
  display: flex;
  height: 80px;
  line-height: 80px;
  width: 600px;
}
.title {
  margin-right: 10 px;
  width: 80px;
  text-align: center;
}
.button {
  margin-left: 30px;
  width: 50px;
  height: 30px;
}
</style>